﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication2.Models;
using MvcApplication2.DAL;
using PagedList;

namespace MvcApplication2.Controllers
{
    public class BuildController : Controller
    {
        private ReleaseAutomationContext db = new ReleaseAutomationContext();

        //
        // GET: /Build/

        public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)
        {
        
            {
                ViewBag.CurrentSort = sortOrder;
                ViewBag.TitleSortParm = String.IsNullOrEmpty(sortOrder) ? "Title_desc" : "";
                ViewBag.VersionSortParm = sortOrder == "Version" ? "Version_desc" : "Version";
                ViewBag.StageSortParm = sortOrder == "Stage" ? "Stage_desc" : "Stage";
                ViewBag.BuildTypeSortParm = sortOrder == "BuildType" ? "BuildType_desc" : "BuildType";
               // ViewBag.ProductOwnerIdSortParm = String.IsNullOrEmpty(sortOrder) ? "Owner_desc" : "";
               // ViewBag.StageSortParm = String.IsNullOrEmpty(sortOrder) ? "Stage_desc" : "";
               // ViewBag.BuildTypeSortParm = String.IsNullOrEmpty(sortOrder) ? "Type_desc" : "";
               // ViewBag.DateSortParm = sortOrder == "Date" ? "Date_desc" : "Date";

                if (searchString != null)
                {
                    page = 1;
                }
                else
                {
                    searchString = currentFilter;
                }

                ViewBag.CurrentFilter = searchString;
                
                var builds = from s in db.Builds
                    select s;

                if (!String.IsNullOrEmpty(searchString))
                {
                    builds = builds.Where(s => s.Title.ToUpper().Contains(searchString.ToUpper())
                                                       || s.Version.ToUpper().Contains(searchString.ToUpper())
                                                       || s.ProductOwnerSid.ToUpper().Contains(searchString.ToUpper()));
                }


                switch (sortOrder)
                {
                    case "Title_desc":
                        builds = builds.OrderByDescending(s => s.Title);
                        break;
                    case "Version":
                        builds = builds.OrderBy(s => s.Version);
                        break;
                    case "Version_desc":
                        builds = builds.OrderByDescending(s => s.Version);
                        break;
                    case "Stage":
                        builds = builds.OrderBy(s => s.Stage);
                        break;
                    case "Stage_desc":
                        builds = builds.OrderByDescending(s => s.Stage);
                        break;
                    case "BuildType":
                        builds = builds.OrderBy(s => s.BuildType);
                        break;
                    case "BuildType_desc":
                        builds = builds.OrderByDescending(s => s.BuildType);
                        break;
                    default:
                        builds = builds.OrderBy(s => s.Title);
                        break;
                }

                int pageSize = 3;
                int pageNumber = (page ?? 1);
                return View(builds.ToPagedList(pageNumber, pageSize));

                //return View(buildItems.ToList());
            }
        }

        //
        // GET: /Build/Details/5

        public ActionResult Details(int id = 0)
        {
            Build build = db.Builds.Find(id);
            if (build == null)
            {
                return HttpNotFound();
            }
            return View(build);
        }

        //
        // GET: /Build/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Build/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Build build)
        {
            if (ModelState.IsValid)
            {
                db.Builds.Add(build);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(build);
        }

        //
        // GET: /Build/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Build build = db.Builds.Find(id);
            if (build == null)
            {
                return HttpNotFound();
            }
            return View(build);
        }

        //
        // POST: /Build/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Build build)
        {
            if (ModelState.IsValid)
            {
                db.Entry(build).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(build);
        }

        //
        // GET: /Build/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Build build = db.Builds.Find(id);
            if (build == null)
            {
                return HttpNotFound();
            }
            return View(build);
        }

        //
        // POST: /Build/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Build build = db.Builds.Find(id);
            db.Builds.Remove(build);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}